/* eslint-disable no-unused-vars */
import { createApp } from 'vue';
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import { createPinia } from 'pinia';
import App from './App.vue';
import { useAccountStore } from "./store/AccountStore";
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min";

import "@fortawesome/fontawesome-free/css/fontawesome.min.css";
import "@fortawesome/fontawesome-free/css/solid.min.css";


import managementComponent from "./components/management.vue";
import zstpComponent from "./components/zstp.vue";
import jbzdComponent from "./components/jbzd.vue";
import stsbComponent from "./components/stsb.vue";
import blzjComponent from "./components/blzj.vue";
import zscqComponent from "./components/zscq.vue";
import bzgjComponent from "./components/bzgj.vue";
import notFoundComponent from "./components/not-found.vue";

const routes = [
    {
        path: "/management",
        name: "management",
        component: managementComponent,
        meta: {
            keepAlive: true
        }
    },
    {
        path: "/zstp",
        name: "zstp",
        component: zstpComponent,
        meta: {
            keepAlive:true
        }
    },
    {
        path: "/jbzd",
        name: "jbzd",
        component: jbzdComponent,
        meta: {
            keepAlive: true
        }
    },
    {
        path: "/stsb",
        name: "stsb",
        component: stsbComponent,
        meta: {
            keepAlive: true
        }
    },
    {
        path: "/blzj",
        name: "blzj",
        component: blzjComponent,
        meta: {
            keepAlive: true
        }
    },
    {
        path: "/zscq",
        name: "zscq",
        component: zscqComponent,
        meta: {
            keepAlive: true
        }
    },
    {
        path: "/bzgj",
        name: "bzgj",
        component: bzgjComponent,
        meta: {
            keepAlive: true
        }
    },
    {
        path: "/",
        redirect: { path: "/zstp", redirect: true }
    },
    {
        path: "/error",
        name: "error",
        component: notFoundComponent
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to,from) => {
    // handle page not found
    let isNotFound = true;
    
    for (const i of routes) {
        if (i.path === to.path) {
            isNotFound = false;
            break;
        }
    }

    if (isNotFound) {
        return {
            name: "error",
            replace: true
        };
    }

    // handle unauthorized management page access
    const accountStore = useAccountStore();
    if (to.name === "management" && !accountStore.canVisitManagement) {
        
        return {
            name: "zstp",
            replace: true
        };
    }
});

createApp(App).use(createPinia()).use(router).mount('#hwmi-app');